<template>
	<view class="wrap">
		<!-- <view v-for="(item,index) in list" :key="index">
			<view class="listbox" v-for="(text,indexs) in item.times" :key="indexs">
				<view class="">
					<view class="">场馆名称:{{name.value}}</view>
					<view class="">订单编号:{{item.order_num}}</view>
					<view class="">开始时间:{{text.start_time_text}}</view>
					<view class="">结束时间:{{text.end_time_text}}</view>
					
				</view>
				<view v-if="item.status==1" class="listbutt" @click="gotosy(item)">去使用</view>
				<view v-else-if="item.status==2" class="listbutt" @click="gotosy(item)">部分销核</view>
				<view v-else-if="item.status==5" class="listbutt1" >已销核</view>
			</view>
		</view> -->
		<view style="text-align: center;" v-if="list.length <= 0" class="intext">--暂无数据--</view>
		<view class="content" v-else>
			<view class="item" v-for="(item, index) in list" :key="index">
				<view class="address">
					<u-icon name="map-fill" color="#10c696"></u-icon>
					<view class="text">{{item.place.name}} {{item.place.address}}</view>
				</view>
				<view class="name">
					<view class="img_wrap">
						<image :src="item.place.image" mode=""></image>
					</view>
					<text>{{item.place.name}}</text>
				</view>
				<view class="label">
					<view class="title">预约人：</view>
					<text>{{item.realname}}</text>
				</view>
				<view class="label">
					<view class="title">预约电话：</view>
					<text>{{item.mobile}}</text>
				</view>
				<view class="label">
					<view class="title">订单编号：</view>
					<text>{{item.order_num}}</text>
				</view>
				<view class="label">
					<view class="title">下单时间：</view>
					<text>{{item.pay_time_text}}</text>
				</view>
				<view class="label">
					<view class="title">场地类型：</view>
					<text>{{item.price_info.name}} 包场时间小于2个小时（限进{{item.price_info.number1}}人），包场时间大于等于2个小时（限进{{item.price_info.number2}}人）</text>
				</view>
				<view class="subItem" v-for="(subItem, subIndex) in item.times" :key="subIndex">
					<view class="time">
						<view class="label">
							<view class="title">{{subItem.number}}号场</view>
						</view>
						<view class="label">
							<view class="title">开始时间：</view>
							<text>{{subItem.start_time_text}}</text>
						</view>
						<view class="label">
							<view class="title">结束时间：</view>
							<text>{{subItem.end_time_text}}</text>
						</view>
						<view class="label">
							<view class="title">整租时长：</view>
							<text>{{subItem.hours}}</text>
						</view>
						<view class="label">
							<view class="title">限进人数：</view>
							<text>{{subItem.total_num}}</text>
						</view>
					</view>
					<view class="bottom">
						<view class="btn" v-if="subItem.status == 1" @click="gotosy(item, subItem)">去使用</view>
						<view class="btn tui" v-if="subItem.status == 1" @click="handleRefund(item, subItem)">退款</view>
						<view class="btn gray" v-if="subItem.status == 3">已过期</view>
						<view class="btn gray" v-if="subItem.status == 4">已退款</view>
						<view class="btn gray" v-if="subItem.status == 2 || subItem.status == 5">已核销</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import { ref,onMounted } from 'vue';
	import {onReachBottom} from '@dcloudio/uni-app'
	import request from '../../utils/request';
	onMounted(()=>{
		token.value=uni.getStorageSync('token')
		lng.value=uni.getStorageSync('longitude')
		lat.value=uni.getStorageSync('latitude')
		getData()
		// request.post('Places/lists',{data:{
		// 	lng:lng.value,
		// 	lat:lat.value
		// },header:{
		// 	"token":token.value
		// }}).then(res=>{
		// 	console.log(res);
		// 	name.value=res.data[0].name
		// 	console.log(name.value);
		// }).catch(err=>{
		// 	console.log(err);
		// })
	})
	let list =ref([])
	let token=ref('')
	let lng=ref('')
	let lat=ref('')
	let places_id=ref('')
	let name=ref('')
	let page = ref(1)
	let last_page = ref(1)
	
	onReachBottom(() => {
		if(page.value == last_page.value){
			uni.$u.toast('已经是最后一页了')
		}else{
			page.value++
			getData()
		}
	})
	
	const getData = () => {
		if (page.value == 1) { 
			list.value = []
		}
		request.post('Order/lists',{data:{
			type1:'1',
			page: page.value
		},header:{
			"token":token.value
		}}).then(res=>{
			last_page.value = res.data.last_page
			if (res.code == 1) {
				res.data.data.forEach((item,index) => {
					list.value.push(item)
				})
			} else {
				uni.showToast({
					title:res.msg,
					icon:'none'
				})
			}
		
		}).catch(err=>{
			console.log(err);
		})
	}
	const gotosy=(item, subItem)=>{
		subItem.order_num = item.order_num + '_' + subItem.id
		uni.navigateTo({
			url: '/pages/erwm/erwm?info=' + JSON.stringify(subItem) + '&type=1'
		})
	}
	
	const handleRefund = (item, subItem) => {
		uni.showModal({
			title: '提示',
			content: '是否确认退款？',
			success: function (res) {
				if (res.confirm) {
					uni.requestSubscribeMessage({
						tmplIds: ['ropT_b9f-HPW6onEWhc5q2OTnx62V4aAbM8Xe3kyfP0'],
						success(res) {
							refound(item, subItem)
						},
						fail(err) {
							console.log(err, '失败');
							// 处理订阅失败后的逻辑
						}
					})
				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		});
		
	}
	const refound = (item, subItem) => {
		request.post('Price/refundOrder',{data:{
			order_num:item.order_num + '_' + subItem.id
		},header:{
			"token":token.value
		}}).then(res=>{
			uni.$u.toast(res.msg)
			page.value = 1
			setTimeout(() => {
				getData()
			}, 2000)
		}).catch(err=>{
			console.log(err);
		})
	}
</script>

<style scoped lang="scss">
	.wrap {
		width: 100vw;
		min-height: 100vh;
		height: fit-content;
		padding: 24rpx;
		background-color: #f2f2f2;
		box-sizing: border-box;
		
		.content {
			
			.item {
				padding: 24rpx 24rpx 0;
				margin-bottom: 24rpx;
				background-color: #fff;
				border-radius: 20rpx;
				box-sizing: border-box;
				
				.address {
					display: flex;
					align-items: center;
					padding: 0 0 24rpx;
					border-bottom: 1rpx solid #ddd;
					box-sizing: border-box;
					margin-bottom: 16rpx;
					
					.text {
						width: 90%;
						overflow: hidden;
						white-space: nowrap;
						text-overflow: ellipsis;
						word-break: break-all;
						margin-left: 16rpx;
					}
				}
				
				.name {
					display: flex;
					align-items: center;
					margin-bottom: 16rpx;
					
					.img_wrap {
						width: 96rpx;
						height: 96rpx;
						margin-right: 24rpx;
						border-radius: 100%;
						
						image {
							width: 100%;
							height: 100%;
							border-radius: 100%;
						}
					}
					
					text {
						font-size: 28rpx;
						font-weight: bold;
					}
				}
				
				.label {
					display: flex;
					align-items: center;
					margin-bottom: 16rpx;
				
					.title {
						margin-right: 20rpx;
						font-size: 28rpx;
					}
					
					text {
						flex: 1;
						font-size: 26rpx;
					}
				}
				
				.subItem {
					padding: 24rpx 0;
					border-bottom: 1rpx solid #ddd;
					// display: flex;
					// align-items: center;
					// justify-content: space-between;
				}
				
				.bottom {
					display: flex;
					align-items: center;
					justify-content: flex-end;
					margin-top: 24rpx;
					
					.btn {
						padding: 0 24rpx;
						height: 64rpx;
						line-height: 64rpx;
						color: #fff;
						background-color: #10c696;
						border-radius: 32rpx;
						box-sizing: border-box;
					}
					
					.gray {
						background-color: #afafaf;
					}
					
					.tui {
						background-color: #DE1F28;
						margin-left: 24rpx;
					}
				}
			}
		}
	}
	// .theabox{
	// 	width: 100%;
	// 	height: 100%;
	// 	background-color: #f8f8f8;
	// 	padding-top: 20rpx;
	// }     
	// .listbox{
	// 	width: 710rpx;
	// 	height: 260rpx;
	// 	background-color: #fff;
	// 	margin: 0 auto;
	// 	margin-bottom: 40rpx;
	// 	display: flex;
	// 	align-items: center;
	// 	justify-content: space-between;
	// 	padding-left: 20rpx;
	// }
	// .listbutt{
	// 	width: 140rpx;
	// 	height: 60rpx;
	// 	background-color: #10c696;
	// 	border-radius: 40rpx;
	// 	line-height: 60rpx;
	// 	text-align:center;
	// 	color: #fff;
	// }
	// .listbutt1{
	// 	width: 140rpx;
	// 	height: 60rpx;
	// 	background-color: #afafaf;
	// 	border-radius: 40rpx;
	// 	line-height: 60rpx;
	// 	text-align:center;
	// 	color: #fff;
	// }
</style>
